package com.zxcl.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxcl.dto.HotSearchQueryDTO;
import com.zxcl.dto.PageDTO;
import com.zxcl.utils.AuthUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxcl.domain.HotSearch;
import com.zxcl.mapper.HotSearchMapper;
import com.zxcl.service.HotSearchService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * 描述: []
 * 作者: zxcl
 */
@Service
public class HotSearchServiceImpl extends ServiceImpl<HotSearchMapper, HotSearch> implements HotSearchService{

    /**
     * 分页查询热搜
     *
     * @param pageDTO
     * @param hotSearch
     * @return
     */
    @Override
    public Page<HotSearch> loadHotSearchPage(PageDTO pageDTO, HotSearchQueryDTO hotSearch) {
        Long shopId = AuthUtil.getShopId();
        Page<HotSearch> page=new Page<>(pageDTO.getCurrent(),pageDTO.getSize());
        return getBaseMapper().selectPage(page, new LambdaQueryWrapper<HotSearch>()
                .eq(shopId != 1L, HotSearch::getShopId, shopId)
                .eq(!ObjectUtils.isEmpty(hotSearch.getStatus()), HotSearch::getStatus, hotSearch.getStatus())
                .like(StringUtils.hasText(hotSearch.getTitle()), HotSearch::getTitle, hotSearch.getTitle())
                .like(StringUtils.hasText(hotSearch.getContent()), HotSearch::getContent, hotSearch.getContent())
                .orderByDesc(HotSearch::getSeq)
        );
    }

    /**
     * 新增热搜
     *
     * @param hotSearch
     * @return
     */
    @Override
    public int addHotSearch(HotSearch hotSearch) {
        Long shopId = AuthUtil.getShopId();
        hotSearch.setCreateTime(new Date());
        hotSearch.setShopId(shopId);
        return getBaseMapper().insert(hotSearch);
    }

    /**
     * 搜索店铺热门商品
     * @param number
     * @param shopId
     * @param sort
     * @return
     */
    @Override
    public List<HotSearch> hotSearchMall(Integer number, Long shopId, Integer sort) {

        return getBaseMapper().selectList(new LambdaQueryWrapper<HotSearch>()
                .eq(HotSearch::getShopId,shopId)

        );
    }
}
